﻿Imports ICSharpCode.AvalonEdit
Imports ICSharpCode.AvalonEdit.Highlighting
Imports System.IO
imports System.Windows
imports System.Windows.Controls
Imports Microsoft.Win32
Imports PowerShellWorkBench.modPowerShell
Class WorkBench
    Private customHighlighting As IHighlightingDefinition
    Private WithEvents timer As New Windows.Threading.DispatcherTimer
    Public Shared OutputBox As TextBox
    Public Shared PopupMenu As Menu
    Public Shared LeftRightPanel As AvalonDock.ResizingPanel
    Public Shared Instance As WorkBench
    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Instance = Me
        LeftRightPanel = Me.LeftRightResizingPanel
        PopupMenu = ItemContextMenu
        Dim s As Stream = New IO.FileStream("powershell.xshd", FileMode.Open)
        Dim reader As Xml.XmlReader
        reader = New Xml.XmlTextReader(s)
        customHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(reader, HighlightingManager.Instance)
        HighlightingManager.Instance.RegisterHighlighting("Custom Highlighting", New String() {".ps1", ".psm1", ".psd1"}, customHighlighting)

        OutputBox = txtOutput

        InitializeRunspace()

        timer.Interval = TimeSpan.FromMilliseconds(100)
        timer.Start()

        Dim f As ICSharpCode.AvalonEdit.TextEditor
        Dim d As New AvalonDock.DockableContent
        d.Title = "Untitled"
        f = New ICSharpCode.AvalonEdit.TextEditor
        f.Background = Windows.Media.Brushes.White
        f.SyntaxHighlighting = customHighlighting
        d.Content = f
        d.Tag = Nothing
        RightPane.Items.Add(d)
        AddHandler d.IsVisibleChanged, AddressOf PWBUIHandling.DockableContent_IsVisibleChanged
        d.HideOnClose = False
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Dim dc As AvalonDock.DockableContent
 		dim filename as String=""
        Dim ed As icsharpcode.AvalonEdit.TextEditor
        
        dc = CType(RightPane.SelectedItem, AvalonDock.DockableContent)
        ed = CType(dc.Content, ICSharpCode.AvalonEdit.TextEditor)
		If typeof dc.Tag Is String Then
            filename = CStr(dc.Tag)
		Else
			Dim o As New SaveFileDialog 
			If o.ShowDialog Then
				filename=o.FileName 
                dc.Title = IO.Path.GetFileName(o.FileName)
                dc.Tag = o.FileName
			else
				return 
			End If
		End If
        File.WriteAllText(filename, ed.Text)
   
    End Sub


    Private Sub txtCmdLine_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles txtCmdLine.KeyDown
        If e.Key = Key.Enter Then
            InvokeString(txtCmdLine.Text)
            txtCmdLine.Text = ""
        End If
    End Sub

    Private Sub timer_tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer.Tick
        ProcessEvents()

    End Sub


    Private Sub btnExecute_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnExecute.Click
        Dim dc As AvalonDock.DockableContent
        Dim filename As String = ""
        Dim ed As ICSharpCode.AvalonEdit.TextEditor

        dc = CType(RightPane.SelectedItem, AvalonDock.DockableContent)
        ed = CType(dc.Content, ICSharpCode.AvalonEdit.TextEditor)
        InvokeString(ed.Text)
    End Sub

    Private Sub Window1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
        If e.Key = Key.F5 Then
            btnExecute_Click(Nothing, Nothing)
        End If
    End Sub

  

    Public Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNew.Click
        Dim f As ICSharpCode.AvalonEdit.TextEditor
        Dim d As New AvalonDock.DockableContent
        d.Title = "Untitled"
        f = New ICSharpCode.AvalonEdit.TextEditor
        f.Background = Windows.Media.Brushes.White
        f.SyntaxHighlighting = customHighlighting
        d.Content = f
        d.Tag = Nothing
        d.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
        RightPane.Items.Add(d)
        RightPane.SelectedItem = d
        d.HideOnClose = False
    End Sub

    Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnOpen.Click
        Dim o As New OpenFileDialog
        If o.ShowDialog Then
            Dim f As ICSharpCode.AvalonEdit.TextEditor
            Dim d As New AvalonDock.DockableContent
            d.Title = IO.Path.GetFileName(o.FileName)
            f = New ICSharpCode.AvalonEdit.TextEditor
            f.Load(o.FileName)
            f.Background = Windows.Media.Brushes.White
            f.SyntaxHighlighting = customHighlighting
            d.Content = f
            d.Tag = o.FileName
            d.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
            RightPane.Items.Add(d)
            RightPane.SelectedItem = d
            d.HideOnClose = False
        End If

    End Sub

     
End Class
